<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/3/30 14:30
 * +----------------------------------------------------------------------
 */
declare (strict_types = 1);

namespace app\mall\model\traits;

use app\mall\model\MallStore;
use think\model\relation\BelongsTo;

/**
 * 获取门店
 * trait StoreTrait
 * @package app\mall\model\traits
 * @mixin \think\Model
 * @property integer $store_id
 */
trait StoreTrait
{

    /**
     * 获取门店
     * @return BelongsTo
     */
    public function store(): BelongsTo
    {
        return $this->belongsTo(MallStore::class, 'store_id', 'id');
    }

    public function storeBind()
    {
        return $this->store()->bind([
            'store_name' => 'name',
            'store_logo' => 'logo',
        ]);
    }

    /**
     * 获取门店名称
     * @return string
     */
    public function getStoreNameAttr()
    {
        return $this->getStoreInfo('name');
    }

    /**
     * 获取门店logo
     * @return string
     */
    public function getStoreLogoAttr()
    {
        return $this->getStoreInfo('logo');
    }

    /**
     * 获取门店等级
     * @return string
     */
    public function getStoreLevelTextAttr()
    {
        return $this->getStoreInfo('level_text');
    }

    private function getStoreInfo($key)
    {
        $model = $this->store();
        if(!$model){
            return '';
        }
        return $model->value($key);
    }

}